*******************************************************************************************
************ DATA PREPARATION: Local councilors 1919-1967 *********************************
*******************************************************************************************
use "dta\Kommunevalg_1898_1967_fordelt.dta", clear

foreach i in 1919 1922 1925 1928 1937 1945 1947 1951 1955 1959 1963 1967 {
    gen fraction_women`i' = Representanter_kvinner_`i' / Representanter_ialt_`i'
}

keep kommnr fraction_women* 
reshape long fraction_women, i(kommnr) j(year)
collapse fraction_women, by(year)
gen type=1
save dta\temp1.dta, replace

*******************************************************************************************
************ DATA PREPARATION: Local councilors 1971-2019 *********************************
*******************************************************************************************
use dta\FivaHalseNatvik2023.dta, clear
keep if yele==1
replace year=year-1
collapse FemaleCouncilMembers, by(year)
rename FemaleCouncilMembers fraction_women
gen type=1
save dta\temp2.dta, replace

*******************************************************************************************
************ DATA PREPARATION: Local councilors 2023 **************************************
*******************************************************************************************
use "dta\LCD_v13.dta" , clear
keep if elected_loc==1 & year==2023
collapse female, by(knr_loc)
gen year=2023
collapse female, by(year)
rename female fraction_women
gen type=1
save dta\temp3.dta, replace

*******************************************************************************************
************ DATA PREPARATION: Mayors 1919-2023 *******************************************
*******************************************************************************************
use "dta\LCD_v13.dta" , clear
keep if mayor_loc==1
keep if year>1970
collapse female, by(year)
rename female fraction_women
set obs 28

replace year = 1922 in 15
replace year = 1925 in 16
replace year = 1928 in 17
replace year = 1931 in 18
replace year = 1934 in 19
replace year = 1937 in 20
replace year = 1945 in 21
replace year = 1947 in 22
replace year = 1951 in 23
replace year = 1955 in 24
replace year = 1959 in 25
replace year = 1963 in 26
replace year = 1967 in 27
replace year = 1919 in 28

*** There are no women mayors elected between 1925 and 1971 (see https://no.wikipedia.org/wiki/Julie_Caspersen)
replace fraction_women=0 if year<1970  
replace fraction_women=1/(730) if year==1925  /* https://snl.no/Aasa_Helgesen ; one out of 730 mayors */
gen type=2
save dta\temp4.dta, replace

******************************************************************************************
**************** DATA PREPARATION: Members of Parliament 1921-2023 & Cabinet 1921-2024 ***
******************************************************************************************
use "dta\DataSet_v28.dta", clear

keep if year>1920 

*** Manually adding new cabinet members up to June, 2024
replace cabinet = 1 if candidatename_ed=="Erling Sande" & year==2021
replace cabinet = 1 if candidatename_ed=="Geir Pollestad" & year==2021
replace cabinet = 1 if candidatename_ed=="Cecilie Myrseth" & year==2021
replace cabinet = 1 if candidatename_ed=="Marianne Sivertsen Næss" & year==2021
*replace cabinet = 1 if candidatename_ed=="Terje Lien Aasland" & year==2021 /* not running in 2021 */
*replace cabinet = 1 if candidatename_ed=="Andreas Bjelland Eriksen" & year==2021 /* not running in 2021 */
*replace cabinet = 1 if candidatename_ed=="Lubna Jaffery" & year==2021 /* never running */
*replace cabinet = 1 if candidatename_ed=="Kari Nessa Nordtun" & year==2021 /* never running */
*replace cabinet = 1 if candidatename_ed=="Oddmund Løkensgard Hoel" & year==2021 /* never running */

gen femaleXelected=female*elected
gen femaleXcabinet=female*cabinet

collapse female (sum) elected (sum) cabinet (sum) femaleXelected (sum) femaleXcabinet, by(year)
gen female_MPs=femaleXelected/elected
gen female_cabinet=femaleXcabinet/cabinet
replace female_MPs=0 if female_MP==.

***** The first Norwegian government with a female member was Einar Gerhardsen's coalition government from June 25 to November 5, 1945.
***** Source: https://www.regjeringen.no/no/om-regjeringa/tidligere-regjeringer-og-historie/statistikk/menn-og-kvinner-i-norske-regjeringer-sid/id586906/
replace female_cabinet= 0 if year<1945

save "dta\temp5.dta", replace

******************************************************************************************
**************** DATA PREPARATION: List-leaders 1921-2023  *******************************
******************************************************************************************
use "dta\DataSet_v28.dta", clear
keep if year>1920 & rank==1
collapse female, by(year)
rename female female_rank1
save "dta\temp6a.dta", replace


******************************************************************************************
**************** DATA PREPARATION: Candidates 1921-2023  *********************************
******************************************************************************************
use "dta\DataSet_v28.dta", clear
keep if year>1920 
collapse female, by(year)
rename female female_candidates
save "dta\temp6b.dta", replace


*******************************************************************************************
************ DATA PREPARATION: Regional councilors 1975-2023 ******************************
*******************************************************************************************
/*
use "dta\LCD_v13.dta" , clear
keep if elected_reg==1 
collapse female, by(year cnr_reg)
collapse female, by(year)
rename female fraction_women
gen type=6
save dta\temp_reg1.dta, replace

*******************************************************************************************
************ DATA PREPARATION: Regional mayors 1975-2023 **********************************
*******************************************************************************************
use "dta\LCD_v13.dta" , clear
keep if mayor_reg==1 
collapse female, by(year cnr_reg)
collapse female, by(year)
rename female fraction_women
gen type=7
save dta\temp_reg2.dta, replace
*/
******************************************************************************************
**************** APPEND DATA SETS ********************************************************
******************************************************************************************
use "dta\temp1.dta"
append using dta\temp2.dta
append using dta\temp3.dta
append using dta\temp4.dta

**** append first to get MPs
append using "dta\temp5.dta"
replace fraction_women = female_MPs if type ==.
replace type = 3 if type==. 

**** ... then append again to get cabinet
append using "dta\temp5.dta"
replace fraction_women = female_cabinet if type ==.
replace type = 4 if type ==. 

**** ... then append to get list leaders 
append using "dta\temp6a.dta"
replace fraction_women = female_rank1 if type ==.
replace type = 5 if type==. 

**** ... then append to get candidates
append using "dta\temp6b.dta"
replace fraction_women = female_candidates if type ==.
replace type = 6 if type==. 

*append using dta\temp_reg1.dta
*append using dta\temp_reg2.dta

keep year type fraction_women

*****In the 2023--2027 election period, 40\% of local councilors and 37\% of local mayors are women. 
li if year==2023

save "dta\LocalNationalWomenOverTime.dta", replace
